package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import chessBoard.BoardPanel;
import chessBoard.InfoPanel;
import chessBoard.Style12;
import connection.Reader;

/**
 * Okno pro sachovnici
 * 
 * @author dove
 * 
 */
public class BoardWin extends InternalFrame implements ComponentListener,
		ActionListener {

	private static final long serialVersionUID = 1L;
	private int x;
	private int y;
	private Style12 board;
	private int windowFrameWidth = 4;
	private int windowFrameHeight = 23;
	private Dimension size;
	private InfoPanel infoPanel;
	private JButton abort;
	private JButton resign;
	private JButton draw;
	private JButton adjourn;
	private BoardPanel boardPanel;

	/**
	 * Nastavuje nazev okna
	 * 
	 * @param name
	 *            nazev okna
	 */
	public BoardWin(String name) {
		super(name);
	}

	/**
	 * Pripojuje sachovnici
	 */
	public void connect() {
		board.connect();
	}

	/**
	 * Nastavuje okno
	 */
	public void setWindow() {
		board = new Style12();

		size = board.getBoard().getSize();
		size.width += windowFrameWidth + 200;
		size.height += windowFrameHeight + 30;
		setSize(size);

		JPanel buttonPanel = new JPanel();
		abort = new JButton(messages.getString("Abort"));
		resign = new JButton(messages.getString("Resign"));
		draw = new JButton(messages.getString("Draw"));
		adjourn = new JButton(messages.getString("Adjourn"));
		abort.addActionListener(this);
		resign.addActionListener(this);
		draw.addActionListener(this);
		adjourn.addActionListener(this);
		Dimension butSize = new Dimension(130, 20);
		abort.setPreferredSize(butSize);
		resign.setPreferredSize(butSize);
		draw.setPreferredSize(butSize);
		adjourn.setPreferredSize(butSize);
		buttonPanel.add(abort);
		buttonPanel.add(resign);
		buttonPanel.add(draw);
		buttonPanel.add(adjourn);

		infoPanel = board.getInfoPanel();
		boardPanel = board.getBoard();
		this.addComponentListener(this);
		this.add(boardPanel);
		this.add(infoPanel);
		this.add(buttonPanel, BorderLayout.PAGE_END);
	}

	public void componentHidden(ComponentEvent arg0) {
	}

	public void componentMoved(ComponentEvent arg0) {

	}

	public void componentResized(ComponentEvent arg0) {
		logger.debug("resized");
		logger.debug(this.getSize());
		x = this.getSize().width - windowFrameWidth - 200;
		y = this.getSize().height - windowFrameHeight - 30;
		logger.debug(x + "," + y);
		board.getBoard().setFieldSize(new Dimension(x / 8, y / 8));
		infoPanel.setPosition(x);
		setSize(this.getSize());
	}

	public void componentShown(ComponentEvent arg0) {
	}

	public void actionPerformed(ActionEvent arg0) {
		Object action = arg0.getSource();
		if (action.equals(abort)) {
			Reader.sendString("abort");
			logger.debug("ABORT");
		}
		if (action.equals(resign)) {
			Reader.sendString("resign");
			logger.debug("RESIGN");
		}
		if (action.equals(draw)) {
			Reader.sendString("draw");
			logger.debug("DRAW");
		}
		if (action.equals(adjourn)) {
			Reader.sendString("adjourn");
			logger.debug("ADJOURN");
		}
	}
}
